package com.donglan.pethome.quartz.service.impl;

import com.donglan.pethome.basic.util.QuartzUtils;
import com.donglan.pethome.quartz.domain.QuartzJobInfo;
import com.donglan.pethome.quartz.job.QuartzJob;
import com.donglan.pethome.quartz.service.QuartzService;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

/**
 * @Author: zhangdonghui
 * @Date: 2022/3/18 21:15
 * 定时任务
 */
@Service
public class QuartzServiceImpl implements QuartzService {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Override
    public void addJob(QuartzJobInfo quartzJobInfo) {

        // 获取 scheduler 对象
        Scheduler scheduler = schedulerFactoryBean.getObject();
        // 动态添加任务
        // addJob(Scheduler sched, String jobName, Class cls, Object params, String time)
        QuartzUtils.addJob(scheduler,quartzJobInfo.getJobName(), QuartzJob.class,quartzJobInfo,quartzJobInfo.getCronj());
    }

    @Override
    public void removeJob(String jobName) {
        // 获取 scheduler 对象
        Scheduler scheduler = schedulerFactoryBean.getObject();
        // 移除一个任务
        QuartzUtils.removeJob(scheduler,jobName);
    }
}
